/**
 * Copyright (C), 2001-2023, www.bosssof.com.cn
 * FileName: UserController
 * Author: Administrator
 * Date: 2023-04-03 14:11:25
 * Description:
 * <p>
 * History:
 * <author> Administrator
 * <time> 2023-04-03 14:11:25
 * <version> 1.0.0
 * <desc> 版本描述
 */
package com.bosssoft.quickstart.demo.controller;

import com.bosssoft.quickstart.demo.api.UserApi;
import com.bosssoft.quickstart.demo.pojo.dto.IdVersion;
import com.bosssoft.quickstart.demo.pojo.dto.UserDTO;
import com.bosssoft.quickstart.demo.pojo.query.UserQuery;
import com.bosssoft.quickstart.demo.pojo.vo.CommonPage;
import com.bosssoft.quickstart.demo.pojo.vo.CommonResponse;
import com.bosssoft.quickstart.demo.service.UserService;
import com.bosssoft.quickstart.demo.utils.CommonResponseUtils;
import com.bosssoft.quickstart.demo.utils.MyBeanUtil;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

/**
 * @ClassName: UserController
 * @Description: 当前版本针对前端的页面实现相应的功能接口,特别说明如下：
 * 1、接口注解做参数非空等检查
 * 2、控制层不处理异常，异常交给统一异常处理
 * 3、控制层的返回参数被统一包转为公共类型
 * @Author: Administrator
 * @Date: 2023-04-03 14:11
 **/
@RestController
public class UserController implements UserApi {
    /**
     * 用户service 包括了用户的所有的业务
     */
    @Autowired
    private UserService userService;
    @Override
    public CommonResponse<Boolean> add(@NotNull @Valid  @RequestBody @ApiParam(value = "用户数据", required = true) UserDTO userDTO) {
        return CommonResponseUtils.booleanResult(userService.add(userDTO));
    }

    @Override
    public CommonResponse<Boolean> remove(@PathVariable @NotNull Long id) {
        return CommonResponseUtils.booleanResult(userService.remove(id));
    }

    @Override
    public CommonResponse<Boolean> remove(@NotNull @RequestParam("id") Long id,@NotNull @RequestParam("version") Long version) {
        return CommonResponseUtils.booleanResult(userService.remove(id,version));
    }

    @Override
    public CommonResponse<Boolean> remove(@NotEmpty @RequestBody IdVersion[] idVersions) {
        return CommonResponseUtils.booleanResult(userService.remove(idVersions));
    }

    @Override
    public CommonResponse<Boolean> update(@NotNull @Valid @RequestBody UserDTO userDTO) {

        return CommonResponseUtils.booleanResult(userService.update(userDTO));
    }

    @Override
    public CommonResponse<CommonPage<UserDTO>> search(@NotNull @Valid UserQuery userQuery) {
        return CommonResponseUtils.success(userService.search(userQuery));
    }

    @Override
    public CommonResponse<UserDTO> findById(@PathVariable @NotNull Long id) {
        return  CommonResponseUtils.success(MyBeanUtil.copyProperties(userService.findById(id),UserDTO::new));
    }
}
